package com.lee.demo1.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.mylib.www.CsvUtils;
import cn.com.mylib.www.HttpUtils;

import com.lee.demo1.business.SearchBusiness;


@Controller
@RequestMapping("search")
public class SearchController {
	
	private static Logger logger = LoggerFactory.getLogger(SearchController.class);
	
	@Resource
	private SearchBusiness searchBusiness;
	
	
	@RequestMapping
	public String load(){
		return "admin/search/search";
	}
	
	@RequestMapping(params = "method=search")
	public String search(Model model, HttpServletRequest request){
		
		String keyword = request.getParameter("keyword");
		//String encodeKw = URLEncoder.encode( keyword,"utf-8");
		logger.debug("---------------" + keyword);
		//logger.debug("---------------" + encodeKw);
		logger.debug("---------------" + searchBusiness.getSearchServer1Url());
		//http://localhost:8983/solr/collection1/select?q=drug_names%3A%E5%90%8D%E7%A7%B0&wt=csv
		//组合URL
		StringBuilder sb = new StringBuilder();
		sb.append(searchBusiness.getSearchServer1Url());
		sb.append("q=");
		try {
			sb.append("drug_names:" + URLEncoder.encode( keyword, "utf-8" ));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		sb.append("&wt=csv");//solr 返回 csv格式结果
		
		/*String ste = new String(HttpUtils.getResponseContents("http://localhost:8983/solr/collection1/select?q=drug_names%3A%E5%90%8D%E7%A7%B0&wt=csv"));*/
		logger.debug("---------------" + sb.toString());
		String csvStr = null;
		try {
			csvStr = HttpUtils.getResponseContents(sb.toString());
		} catch (MalformedURLException e) {
			model.addAttribute("message", "MalformedURLException");
		} catch (IOException e) {
			model.addAttribute("message", "IOException");
			logger.error("solr服务未启动，请启动服务后使用。");
		}
		if(StringUtils.isNotBlank(csvStr)){
			List<Map<String, Object>> list = CsvUtils.csvReader(csvStr);
			model.addAttribute("result", list);
		}
		model.addAttribute("keyword", keyword);
		/*for(int i=0; i<list.size(); i++){
			Map map = list.get(i);
			 Set set =map.entrySet();
		       Iterator it=set.iterator();
		       while(it.hasNext()){
		           Map.Entry<String, String>  entry=(Entry<String, String>) it.next();
		           System.out.println(entry.getKey()+":"+entry.getValue());
		           
		       }
		}*/
		
		return "admin/search/result";
		
	}
	
	
	@RequestMapping(params = "method=getCompleteData")
	@ResponseBody
	public String getCompleteData(){
		//数据管理在数据库中，并能生成缓存文件，共页面调用，是缓存在服务器端还是客户端？
		//拼音输入后，显示汉字？
		return searchBusiness.getCompleteData();
	}
	
}
